#include "TIMER.h"
#include "led.h"
//==============TIMx中断=======================
void TIMx_Int_Init(TIM_TypeDef* TIMx,u8 xz,u8 cy,u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	if(TIMx==TIM1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	else if(TIMx==TIM2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	else if(TIMx==TIM3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	else if(TIMx==TIM4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
	
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	if(TIMx==TIM1) TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
	
	///////////////////TIMx///////////////////////////////////////////////////
	if(TIMx==TIM1) NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;  
	else if(TIMx==TIM2) NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;  
	else if(TIMx==TIM3) NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 
	else if(TIMx==TIM4) NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  
	
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =xz;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = cy;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器
	
	TIM_ITConfig(TIMx,TIM_IT_Update,DISABLE ); //使能指定的TIM中断,允许更新中断
	TIM_ClearFlag(TIMx, TIM_FLAG_Update); 
	TIM_Cmd(TIMx, DISABLE);  //使能TIM
}
void TIM3_PWM_GPIO(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0|GPIO_Pin_1;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		    // 复用推挽输出
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6|GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		    // 复用推挽输出
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIMx_pwm(TIM_TypeDef* TIMx,u16 arr,u16 psc,u8 ch1,u8 ch2,u8 ch3,u8 ch4)
{
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	
	if(TIMx==TIM2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	else if(TIMx==TIM3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	else if(TIMx==TIM4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  	
	TIM_TimeBaseStructure.TIM_Period = arr-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc-1; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;	
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
	
	if(ch1==1){
	TIM_OC1Init(TIMx, &TIM_OCInitStructure);	
	TIM_OC1PreloadConfig(TIMx, TIM_OCPreload_Enable);}
	if(ch2==1){
	TIM_OC2Init(TIMx, &TIM_OCInitStructure);	
	TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);}
	if(ch3==1){
	TIM_OC3Init(TIMx, &TIM_OCInitStructure);	
	TIM_OC3PreloadConfig(TIMx, TIM_OCPreload_Enable);}
	if(ch4==1){
	TIM_OC4Init(TIMx, &TIM_OCInitStructure);	
	TIM_OC4PreloadConfig(TIMx, TIM_OCPreload_Enable);}
 
	TIM_Cmd(TIMx, ENABLE);          
					 
}
/*================================================================ 
  
			TIM中断

=================================================================*/
u16 t_2=0,T1zd=0;

void TIM2_IRQHandler(void)
{ 		    	 
    if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
    {
	    TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
	    t_2=(t_2+1)%100;  
	    if( t_2<=50)LED3=1;    
	    else  LED3=0;
    }
}
void TIM3_IRQHandler(void)
{ 		    	 
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
    {
	    TIM_ClearITPendingBit(TIM3, TIM_IT_Update );
	    t_2=(t_2+1)%100;
	    if( t_2<=50)LED3=1;    
	    else  LED3=0;
           	    
    }
}
void TIM4_IRQHandler(void)
{ 		    	 
    if(TIM_GetITStatus(TIM4,TIM_IT_Update)!=RESET)
    {
	    TIM_ClearITPendingBit(TIM4, TIM_IT_Update );
	    t_2=(t_2+1)%100;
	    if( t_2<=50)LED3=1;    
	    else  LED3=0;
          	    
    }
}
